package com.glgroup;
import com.google.common.io.*;
import com.google.common.base.*;
//import com.google.common.io.Closeables;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Scanner;
import java.util.regex.Pattern;


import   org.apache.commons.io.FileUtils;

public class Auto {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//TODO write code to get all Folders and outer loop on folders
		
	//	File parentdirectory = new File("c:/Users/InnovationHero/Desktop/pathdata/data1/");
//		renameAllFolders(parentdirectory);
		
//		File[] listOfFiles = parentdirectory.listFiles();
		
		// Loop 
//		for(int i = 0; i <listOfFiles.length; i++){
		
//		File foldername = new File( parentdirectory + "/" + listOfFiles[i].getName());
//		renameAllFiles(foldername);
		
//		}
		 
		File datafile = new File("c:/Users/InnovationHero/Desktop/pathdata/data1.txt");
		
		try {
			zabato(datafile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("7'alas keda ya me3alem!");
	}
	
	static void zabato(File datafile ) throws IOException{
		//	Charset charset = Charset.forName("US-ASCII");
		FileInputStream fis;
		int number = 0;
		try {
			System.out.println("Reading from file.");
		    StringBuilder text = new StringBuilder();
		    String NL = System.getProperty("line.separator");
		    Scanner scanner = new Scanner(new FileInputStream(datafile), "UTF-8");
		    try {
		       if (scanner.hasNextLine()){
		    	//while (scanner.findInLine(".").endsWith(".*")){
		        //text.append(scanner.nextLine() + NL);
				//scanner.findInLine(pattern.matcher("."));
		        System.out.println("The Number of files is outer " +  number);
		        // law feha .* yageb the current line weyazabato
		        
		    if (scanner.findInLine("f:/").contains(".*")){
		    	   number = number + 1;
		    	   System.out.println("The Number of files= is ####################### " +  number);
		       } 
		      }
		    }
		    finally{
		      scanner.close();
		    }
		    //System.out.println("Text read in: " + text);
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		
		System.out.println("el file etzabat!");
	}
	

	static void renameAllFiles(File foldername){	
		
		File[] listOfFiles = foldername.listFiles();		
		// Loop
		
		
		for (int i = 0; i < listOfFiles.length; i++){
			if (listOfFiles[i].getName().contains(".pdf")) {
				//if (listOfFiles[i].getName().contains(".*")) { for all files .pdf, xls, ppt, etc.
			if(listOfFiles[i].isFile()){
				System.out.println("File "+ listOfFiles[i].getName());
				
				// Working with files
			
				String filename = listOfFiles[i].getName();
				File currentFile = new File( foldername + "/" + filename);
			
				// Remove unwanted long and stupied file description 
				Iterable<String> filesplitted = Splitter.onPattern(" - ").split(filename);
				
				Object renamedfile = filesplitted.iterator().next();
				System.out.println(renamedfile);
if (renamedfile.toString().contains("pdf")){}else {
	// Project Company name Technip 
//	if (renamedfile.toString().contains("Technip")) {
//		renamedfile.toString().
//	} else if (){}
//	else if () {}
				File renamedFile = new File(foldername.toString() + "/" + renamedfile + ".pdf");
				System.out.println(renamedFile.toString());
				// Rename file (or directory)
				boolean success = currentFile.renameTo(renamedFile);
				
				if (success) {
				    // File was not successfully renamed
					System.out.println("File successfully changed");
				} else {System.out.println("File not successfully changed");}
			}
							
			}// Note Needed really  
			else if (listOfFiles[i].isDirectory()){
				
				System.out.println("Directory " + listOfFiles[i].getName());
			}
		}
		}

	}
	
	static void renameAllFolders(File ParentDirectory){
		
		
		File[] listOfFiles = ParentDirectory.listFiles();
		// Loop 
		for(int i = 0; i <listOfFiles.length; i++){
			if (listOfFiles[i].isDirectory()){
				System.out.println("Directory "+ listOfFiles[i].getName());
				
				// Working with folders
				
				// String foldername = "14030-TNO-BUR-T0310";
				String foldername = listOfFiles[i].getName();
				// File currentFolder = new File("c:/Users/InnovationHero/Desktop/glpathissue/" + foldername);
				File currentFolder = new File(ParentDirectory + "/" + foldername);
				System.out.println(currentFolder.toString());
				// File renamedFolder = new File("c:/Users/InnovationHero/Desktop/glpathissue/" + foldername.substring(14));
				// File renamedFolder = new File(ParentDirectory +"/"+ foldername.substring(14)); // Technip
				File renamedFolder = new File(ParentDirectory +"/"+ foldername.substring(19)); // Allseas
				System.out.println(renamedFolder.toString());
				boolean success = currentFolder.renameTo(renamedFolder);
				if (success) {
				    // File was successfully renamed
					System.out.println("Folder successfully renamed");
				} else {System.out.println("Folder was not successfully renamed");}
			} else {System.out.println("Finshed");}
		}
		
	}	
	 static void renameDirectory(String fromDir, String toDir) {

	    File from = new File(fromDir);

	    if (!from.exists() || !from.isDirectory()) {

	      System.out.println("Directory does not exist: " + fromDir);
	      return;
	    }

	    File to = new File(toDir);

	    //Rename
	    if (from.renameTo(to))
	      System.out.println("Directory Renaming: Success!");
	    else
	      System.out.println("Directory Renaming: Error");
	      

	  }
}
